package com.wanmait.shoppingsys.controller.manage;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.multipart.FileRenamePolicy;
import com.wanmait.shoppingsys.dao.ManageDAO;
import com.wanmait.shoppingsys.dao.SpecialtyDAO;
import com.wanmait.shoppingsys.dao.SpecialtyManagePagerDAO;
import com.wanmait.shoppingsys.dao.TypeDAO;
import com.wanmait.shoppingsys.util.ManagePager;
import com.wanmait.shoppingsys.vo.City;
import com.wanmait.shoppingsys.vo.Specialty;
import com.wanmait.shoppingsys.vo.Type;

@WebServlet("/SpecialtyManageController")
public class SpecialtyManageController extends HttpServlet
{
	
 private SpecialtyDAO specialtyDAO=SpecialtyDAO.getDAO();
 private SpecialtyManagePagerDAO specialtymanagepagerDAO=new SpecialtyManagePagerDAO();
  private ManageDAO manageDAO=new ManageDAO();
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
 @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	 request.setCharacterEncoding("UTF-8");
	 String action=request.getParameter("action");
	 if(action==null)
	 {
		 this.list(request,response);
		 return;
	 }
	 switch(action) {
	 case "list":
		 this.list(request, response);
			break;
	 case "show":          
			this.show(request,response);
			break;
	 case "add":              //添加商品
	     this.add(request,response);
	        break;
	 case "listByType":     //通过类型查找商品
	     this.listByType(request,response);
	        break;
	 case "listByProvince":   
		 this.add(request, response);
		 break;
	 case "listByCity":  //通过城市查找商品
		 this.listByCity(request, response);
		 break;
	 case "delete":      //删除商品
		 this.delete(request, response);
		 break;
	 case "delete1":   //逻辑删除商品   恢复商品
		 this.delete1(request, response);
		 break;
	
	 case "save":          //编辑商品输入数据库
		 this.save(request,response);
		 break;
	 /*case "update":
	     this.update(request,response);
	     break;*/
	/* case "deletelist":
		 this.deletelist(request, response);
	  break;*/
	 case "update":         //编辑商品列出表单
		 this.update(request, response);
		 break;
	 
	 case "evaluation":
		 this.evaluation(request, response);
		 break; 
	 
	 case "serach"://关键字查找商品
		 this.serach(request, response);
		 break;
	

	 }
 }
 


 
 protected void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String idStr = request.getParameter("id");      
		Integer id = Integer.parseInt(idStr);
		Specialty specialty = specialtyDAO.findById(id);
		request.setAttribute("specialty", specialty);
		request.getRequestDispatcher("/manage/show.jsp").forward(request, response);
 }
 
 
 
 
 protected void evaluation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	ManagePager managepager = ManagePager.init(request);
	 manageDAO.findAll3(managepager);

	request.getRequestDispatcher("/manage/evaluation.jsp").forward(request, response);
 }
 
	 
 
 
 
 
 protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
	 request.setCharacterEncoding("UTF-8");
	 String specialtyIdStr=request.getParameter("specialtyId");
	 String name=request.getParameter("name");
	 String typeIdStr=request.getParameter("typeId");
	 String cityIdStr = request.getParameter("cityId");
	 String priceStr=request.getParameter("price");
	 String introduce=request.getParameter("introduce");
	 String pic=request.getParameter("pic");

	 Double price=Double.parseDouble(priceStr);
	 Integer specialtyId=Integer.parseInt(specialtyIdStr);
	 Integer typeId=Integer.parseInt(typeIdStr);
	 Integer cityId = Integer.parseInt(cityIdStr);
	 
	 Specialty specialty=new Specialty();
	 specialty.setId(specialtyId);
	 specialty.setName(name);
	 specialty.setPrice(price);
	 specialty.setIntroduce(introduce);
	 specialty.setPic(pic);
	 
	 Type type=new Type();
	 type.setId(typeId);
	 specialty.setType(type);
	 
	 City city = new City();
	 city.setId(cityId);
	 specialty.setCity(city);
	 
	 
	 specialtyDAO.update(specialty);
	 response.sendRedirect("/ShoppingSys/SpecialtyManageController?action=list");
 }
 
	
 /*protected void deletelist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
	 ManagePager managepager = ManagePager.init(request);
	 manageDAO.findAll(managepager);
	 request.getRequestDispatcher("/manage/deletelist.jsp").forward(request, response);
 }*/

 protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
	 ManagePager managepager = ManagePager.init(request);
	 specialtymanagepagerDAO.findAll(managepager);
	 request.getRequestDispatcher("/manage/list.jsp").forward(request, response);
 }
 
 protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//	Admin admin=(Admin)request.getSession().getAttribute("admin");
//	if(admin==null)
//	{
//		response.sendRedirect("/ShoppingSys/err.jsp");
//		return;
//	}
	String name=request.getParameter("name");
	String typeIdStr = request.getParameter("typeId");
	String cityIdStr=request.getParameter("cityId");
	String priceStr=request.getParameter("price");
	String pic=request.getParameter("pic");
	String introduce=request.getParameter("introduce");
	
	Double price=Double.parseDouble(priceStr);
	Integer typeId=Integer.parseInt(typeIdStr);
	Integer cityId=Integer.parseInt(cityIdStr);
	
	Specialty specialty=new Specialty();
	specialty.setName(name);
	specialty.setPrice(price);
	specialty.setPic(pic);
	specialty.setIntroduce(introduce);
	
	
	City city=new City();
	city.setId(cityId);
	specialty.setCity(city);
	
	Type type=new Type();
	type.setId(typeId);
	specialty.setType(type);
	
	
//	specialty.setAdminId(admin.getId());
	
	specialtyDAO.insert(specialty);
	response.sendRedirect("/ShoppingSys/SpecialtyManageController?action=list");
 }
 
 protected void listByType(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
 {
	 
	
	ManagePager managepager = ManagePager.inot(request);
	specialtymanagepagerDAO.findByTypeId(managepager);
	request.getRequestDispatcher("/manage/list.jsp").forward(request, response);
 }
 
 protected void listByProvince(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
 {
	 String provinceIdStr=request.getParameter("provinceId");
	 Integer provinceId=Integer.parseInt("provinceIdStr");
	 List<Specialty> specialtys=specialtyDAO.findAllByProvinceId(provinceId);
      request.setAttribute("specialtys", specialtys);
	 request.getRequestDispatcher("/manage/list.jsp").forward(request, response);
 }
 
 protected void listByCity(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
 {
	 ManagePager managepager = ManagePager.inat(request);
	specialtymanagepagerDAO.findByCityId(managepager);
	request.getRequestDispatcher("/manage/list.jsp").forward(request, response);
 }
 
 class MyFileRenamePolicy implements FileRenamePolicy{

		@Override
		public File rename(File file) {
			String oldName =file.getName();
			String ext = oldName.substring(oldName.lastIndexOf('.'));
			String filename = UUID.randomUUID().toString()+ext;
			File nFile = new File(file.getParentFile(),filename);
			return nFile;
		}
}
 

 protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
	 String specialtyId = request.getParameter("specialtyId");
	 Integer id = Integer.parseInt(specialtyId);
	 manageDAO.delete(id);
	 this.list(request, response);
	
	
 }
 
 protected void delete1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
	 String idStr = request.getParameter("id");
	 Integer id = Integer.parseInt(idStr);
	 String enStr = request.getParameter("en");
	 Integer en = Integer.parseInt(enStr);
	 specialtyDAO.update1(id,en);
 }
 

 
 protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     String idStr = request.getParameter("id");
     Integer id = Integer.parseInt(idStr);
     Specialty specialty = specialtyDAO.findById(id);
     request.setAttribute("specialty", specialty);
	 request.getRequestDispatcher("/manage/update.jsp").forward(request, response);
}
 
 protected void serach(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     ManagePager managepager = ManagePager.init(request);
     manageDAO.findByNote(managepager);
	 request.getRequestDispatcher("/manage/list.jsp").forward(request, response);
}



}
